﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;

namespace ReportDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // 点击导出下拉框，默认直接弹出pdf保存窗口
            DefaultReportViewerExportPdf();

            // 给报表中名为ReportDemoDataSet的数据集关联显示数据，表格控件由rdlc设计器生成
            DataTable table = new DataTable();
            table.Columns.Add("Id", Type.GetType("System.Int32"));
            table.Columns.Add("Name", Type.GetType("System.String"));
            table.Columns.Add("School", Type.GetType("System.String"));
            table.Rows.Add(1, "丁宁", "北体");
            table.Rows.Add(12, "丁宁2", "北体2");
            table.Rows.Add(13, "丁宁232", "北体33");
            ReportDataSource rds = new ReportDataSource();
            rds.Name = "ReportDemoDataSet";
            rds.Value = table;

            // 加上报表的路径
            reportViewer1.LocalReport.ReportEmbeddedResource = "ReportDemo.Report1.rdlc";

            // 给报表的一些显示参数设置值
            reportViewer1.LocalReport.DataSources.Clear();
            reportViewer1.LocalReport.DataSources.Add(rds);
            List<ReportParameter> paramss = new List<ReportParameter>();
            // 给由rdlc设计器生成的参数ReportParameter1设置值
            paramss.Add(new ReportParameter("ReportParameter1", "中国ccc"));
            reportViewer1.LocalReport.SetParameters(paramss);

            //设置打印布局模式,显示物理页面大小
            reportViewer1.SetDisplayMode(Microsoft.Reporting.WinForms.DisplayMode.PrintLayout);
            reportViewer1.ZoomMode = Microsoft.Reporting.WinForms.ZoomMode.Percent;
            reportViewer1.ZoomPercent = 100;
            // 刷新重显报表
            this.reportViewer1.RefreshReport();
        }

        /// <summary>
        /// 当正在导出报表时发生的事件委托
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void reportViewer1_ReportExport(object sender, ReportExportEventArgs e)
        {
            
        }

        /// <summary>
        /// 给ReportViewer的导出下拉框绑定下拉事件
        /// </summary>
        private void DefaultReportViewerExportPdf()
        {
            if (this.reportViewer1.Controls.Find("toolStrip1", true)[0] is ToolStrip toolStrip)
                foreach (var dropDownButton in toolStrip.Items.OfType<ToolStripDropDownButton>())
                    dropDownButton.DropDownOpened += new EventHandler(DropDownButton_DropDownOpened);
        }

        /// <summary>
        /// 当导出下拉框打开时，直接弹出保存对话框。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void DropDownButton_DropDownOpened(object sender, EventArgs e)
        {
            if (sender is ToolStripDropDownButton ddList)
            {
                foreach (var item in ddList.DropDownItems.OfType<ToolStripDropDownItem>())
                {
                    if (item.Text.Trim().ToLower().Contains("excel")|| item.Text.Trim().ToLower().Contains("word"))
                        item.Enabled = false;
                }
                foreach (var item in ddList.DropDownItems.OfType<ToolStripDropDownItem>())
                {
                    if (item.Text.Trim().ToLower().Contains("pdf"))
                    {
                        item.PerformClick();
                        break;
                    }
                }

            }
        }

        private void reportViewer1_ViewButtonClick(object sender, CancelEventArgs e)
        {
            MessageBox.Show("ViewButtonClick");
        }

        private void reportViewer1_MouseClick(object sender, MouseEventArgs e)
        {
            MessageBox.Show("MouseClick");
        }

        private void reportViewer1_Toggle(object sender, CancelEventArgs e)
        {
            MessageBox.Show("Toggle");

        }
    }
}
